﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class ShoppingCart : DataTable
{
    private string taiKhoan;
    public ShoppingCart()
    {
        this.Columns.Add("MaSP", typeof(string));
        this.Columns.Add("TenSP", typeof(string));
        this.Columns.Add("LoaiSP",typeof(int));
        this.Columns.Add("HinhAnh", typeof(string));
        this.Columns.Add("Gia", typeof(double));
        this.Columns.Add("SoLuong", typeof(int)).DefaultValue = 1;
        //this.Columns.Add("GiamGia", typeof(int)).DefaultValue = 0;
        this.Columns.Add("ThanhTien", typeof(double),"Gia*SoLuong");
        this.PrimaryKey = new DataColumn[] { this.Columns["MaSP"] };
    }
    private void ThemSanPham(string MaSp, int SoLuong)//<them>
    {
        DataRow dr = this.Rows.Find(MaSp);
        if (dr != null)
            dr["SoLuong"] = SoLuong + (int)dr["SoLuong"];
        else
        {
            DataTable dt = new DataTable();
            string sql = "select MaSp,TenSp,LoaiSp,HinhAnh,Gia," + SoLuong + " as SoLuong from SanPham where MaSP=@Masp";
            DataProvider.Fill(this, sql, "@Masp", MaSp); 
        }
    }
    public bool Them(string Masp, int soluong, int checksoluong)//1,checksl database
    {

        DataRow dr = this.Rows.Find(Masp);
        if (dr != null)
        {
            if (int.Parse(dr["SoLuong"].ToString()) >= checksoluong)
            {
                throw new Exception("So luong san pham lon hon so hien co!");
            }
            else
            {

                this.ThemSanPham(Masp, soluong);
                return true;
            }

        }
        if (checksoluong < soluong)
        {
            throw new Exception("Đã hết hàng!");
        }
       else
        {
            if (soluong > checksoluong)
            {
                return false;
            }
            try
            {
                this.ThemSanPham(Masp, soluong);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        
    }
    public void capnhat(string MaSp, int SoLuong,int checksoluong)
    {
        DataRow dr = this.Rows.Find(MaSp);
        if (SoLuong > checksoluong)
        {
            throw new Exception("So luong san pham lon hon so hien co!");
        }
        if (int.Parse(dr["SoLuong"].ToString()) <= checksoluong)
        {
            dr["SoLuong"] = SoLuong;
        }
        
    }
    public void Xoa(string Masp)
    {
        try
        {
            DataRow SanPham = this.Rows.Find(Masp);
            this.Rows.Remove(SanPham);
        }
        catch
        {
            throw new Exception("Không có sản phẩm trong giỏ hàng!");
        }
    }
    public double SoTien
    {
        get
        {
            object value = this.Compute("SUM(ThanhTien)", "");
            return value == DBNull.Value ? 0 : (double)value;
        }
    }
    public string TaiKhoan
    {
        set
        {
            taiKhoan = value;
        }
    }
    public double GiamGia
    {
        get
        {
            if (taiKhoan == null)
                return 0;
            else
            {
                ThanhVienCtr thanhVienCtr = new ThanhVienCtr();
                if(thanhVienCtr.KiemTraKHVip(taiKhoan))
                {
                    double percent = double.Parse(DataProvider.ExecuteScalar("select GiaTriGiam from ThamSo where NgayBD<=getDate() AND GETDATE()<=NgayKT ").ToString());
                    object value = this.Compute("SUM(ThanhTien)", "");
                    return value == DBNull.Value ? 0 : (double)value * percent / 100;
                }
                return 0;
            }
        }
    }
    public double TongTien
    {
        get
        {
            object value = this.Compute("SUM(ThanhTien)", "");
            return value == DBNull.Value ? 0 : (double)value- GiamGia;
        }
    }
    
    public int SoSP
    {
        get
        {
            object value = this.Compute("SUM(SoLuong)", "");
            return value == DBNull.Value ? 0 : Int32.Parse(value.ToString());
        }
    }
}